/**
 * @author Lee
 */

var dataSource = [];
var tableView = null;
var lastId = 0;
var isHasNext = 0;

var jdchphm,
    wzts,
    fkje,
    kf;

function showLoadingDialog(msg) {
    $("#loading_dialog p").html(msg);
    $("#loading_dialog").show();
}

function hideLoadingDialog() {
    $("#loading_dialog").hide();
}

$(document).ready(function() {
    showLoadingDialog("请求中...");
    $.ajax({
        url: '../query/get-query-details',
        type: 'GET',
        dataType: 'json',
        data: {
            carNo: "辽BC3920",
            bodyNo:"123123",
        }
    }).done(function(data) {
        console.log(data);
        data = JSON.parse(data);
        if (data.statusCode == 100101) {
            isHasNext = data.hasNext;
            dataSource = data.data;
            jdchphm =  data['jdchphm'];
            wzts = data['wzts'];
            fkje = data['fkje'];
            kf = data['kf'];
            if (dataSource && dataSource.length > 0) {
                refresTableView(true);
            } else {
                $("#list_nodata").show();
            }
        } else {
            //toErrorPage(1, data.msg);
        }
    }).fail(function(error) {
        //toErrorPage(1, "网络请求失败，请重试");
    }).always(function() {
        hideLoadingDialog();
    });
});

function pullUpAction(theScroller) {
    if (isHasNext) {
        $.ajax({
            url: '../query/get-query-details',
            type: 'GET',
            dataType: 'json',
            data: {
                carNo: "辽BC3920",
                bodyNo:"123123",
            }
        }).done(function(data) {
            console.log(data);
            data = JSON.parse(data);
            isHasNext = data.hasNext;
            dataSource = dataSource.concat(data.data);
            theScroller.notifyDatasetChage(false);
        }).fail(function() {
            console.log("error");
        }).always(function() {
            console.log("complete");
        });
    }
}

function refresTableView(isClearAll) {
    if (tableView) {
        console.log("refresTableView 111");
        tableView.notifyDatasetChage(isClearAll);
    } else {
        console.log("refresTableView 222");
        $(".retListView").iscrollListView({}, {
            onPullUp: pullUpAction,
            needPullUpFooter: function() {
                var footerData = {
                    height: 50,
                    dom: $('<div class="pullUp"><span class="pullUpLabel">上拉加载更多</span></div>').on('statuschanged', function(event, status) {
                        switch (status.status) {
                            case 'LSinitF':
                                $(this).find('.pullUpLabel').html('上拉加载更多');
                                break;
                            case 'LSLoadingF':
                                $(this).find('.pullUpLabel').html('加载中');
                                break;
                            case 'LSflipF':
                                $(this).find('.pullUpLabel').html('松开加载更多');
                                break;
                            default:
                        }
                    })[0]
                };
                return footerData;
            },
            needDataCount: function() {
                return dataSource.length + 1;
            },
            needLineHeight: function() {
                return 150;
            },
            onInitFinish: function(scroller) {
                tableView = scroller;
            },
            hasMoreData: function() {
                return isHasNext;
            },
            configItem: function(index) {
                if (index == 0) {
                    var listItem = $($.nano($("#list_header_template").val(), {
                        'jdchphm': jdchphm,
                        'wzts': wzts,
                        'fkje': fkje,
                        'kf': kf
                    }));
                    return listItem;
                } else {
                    var itemData = dataSource[index - 1];
                    itemData.i = index;
                    itemData.processed = itemData.clbj == 0 ? "processed" : "";
                    itemData.clbj = itemData.clbj == 0 ? "已处理" : "未处理";
                    return $($.nano($("#list_item_template").val(), itemData));
                }
            }
        });
        document.addEventListener('touchmove', function(e) {
            e.preventDefault();
        }, false);
    }
}

